package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // shoppingCartDTO  菜品id+口味  或者 是  套餐id
        // 1: 新建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 2: 所需要添加的所有数据设置到 shoppingCart 中
        // 2.1 这是谁的购物项呢 ？ -- 谁登录是谁的
        // 获取用户id   登录的时候把用户id存到令牌中 拦截器解析token取出来了
        // 放到 BaseContext中 可以从 BaseContext取出来
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);//把那菜品id+口味  或者 是  套餐id存到shoppingcart中
        //2.2 判断一下 新加的购物项是否在 数据库表中存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);// dish_id  dish_flavor  setmeal_id     user_id
        // 如果存在 要更新的是份数
       if(shoppingCartList!=null && shoppingCartList.size()==1){//查到了 存在
           // 更新份数
           shoppingCart = shoppingCartList.get(0);//从数据库查出已经有的 购物项信息 赋值给 shoppingCart
           //更新份数
           shoppingCart.setNumber(shoppingCart.getNumber()+1);
           // 执行更新语句
           shoppingCartMapper.update(shoppingCart);
       }else {
           //  如果没有存在就新加购物项
           //你怎么知道是菜品还是套餐
           Long dishId = shoppingCart.getDishId();
           if(dishId!=null){//菜品
               // 根据菜品id 查询菜品信息
               Dish dish = dishMapper.getById(dishId);
               shoppingCart.setName(dish.getName());
               shoppingCart.setImage(dish.getImage());
               shoppingCart.setAmount(dish.getPrice());
           }else{//套餐
               // 查询套餐信息
               Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
               shoppingCart.setName(setmeal.getName());
               shoppingCart.setImage(setmeal.getImage());
               shoppingCart.setAmount(setmeal.getPrice());
           }
           //份数  创建时间
           shoppingCart.setNumber(1);
           shoppingCart.setCreateTime(LocalDateTime.now());

           // 新增
           shoppingCartMapper.insert(shoppingCart);
       }






    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
       // 查询该用户的购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());//用户id
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void clean() {

        shoppingCartMapper.delete(BaseContext.getCurrentId());//根据用户id删除
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 根据用户id+菜品id+口味  或者  用户id+套餐id 查询指定购物项
        ShoppingCart shoppingCart = new ShoppingCart();//定义的时候 作为封装查询条件
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //还差一个 用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断
        if(list!=null && list.size()==1){//
            shoppingCart = list.get(0);// shoppingCart 变成查询出来购物项
            Integer number = shoppingCart.getNumber();
            if(number==1){//再减就是删除
                shoppingCartMapper.deleteById(shoppingCart.getId());
                log.info("该购物项只有一份 减少就是删除");
            }else{
                log.info("大于一份可以减一");
                shoppingCart.setNumber(number-1);
                //更新
                shoppingCartMapper.update(shoppingCart);
            }
        }
    }
}
